###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Libraries/Variables.pm.pl -> Variables library                              #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Variables;

use Data::Dumper;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY {}

###############################################################################
# Update subroutine
sub Update {
  my $self = shift;
  my %in = ("FILE" => "", "VALUES" => {}, @_);

  open(FILE, $in{'FILE'}) || ($self->{'ERROR'} = "Couldn't open FILE for reading. $!" and return);
  my $File = join("", <FILE>);
  close(FILE);
  
  unless ($File =~ /\$(.*?) \= \{ \#\{start\}/i) {
    $self->{'ERROR'} = "Couldn't find start marker";
    return;
  }
  
  my $Variable = $1;
  
  unless ($File =~ /\}\; \#\{end\}/i) {
    $self->{'ERROR'} = "Couldn't find end marker";
    return;
  }

  unless ($File =~ /\$$Variable \= \{ \#\{start\}(.*)\}\; \#\{end\}/is) {
    $self->{'ERROR'} = "Couldn't retrieve existing data";
    return;
  }
  
  my $Data = $1;  
     $Data = eval("return { $Data };");
  
  foreach my $key (keys %{ $in{'VALUES'} }) {
    $Data->{$key} = $in{'VALUES'}->{$key};
  }

  my $Dumper = Data::Dumper->new([$Data], [$Variable]);
     $Dumper->Indent(2);
     $Dumper->Purity(1);
     $Dumper->Useqq(1);
     $Dumper->Quotekeys(1);
  my $Dump = $Dumper->Dump();
  
  my @Lines = split(/\n/, $Dump);
  
  $Lines[0]       .= " #{start}";
  $Lines[$#Lines] .= " #{end}";
  
  $Dump = join("\n", @Lines);
  
  $File =~ s/\$$Variable \= \{ \#\{start\}(.*)\}\; \#\{end\}/$Dump/is;
  
  open(FILE, ">$in{'FILE'}") || ($self->{'ERROR'} = "Couldn't open FILE for writing. $!" and return);
  print FILE $File;
  close(FILE);
  
  return 1;
}

###############################################################################
# ListDir subroutine
sub ListDir {
  my $self = shift;
  my %in = (
    "DIR"       => "",
    "TYPE"      => "", # VAR|CLASS
    "PACKAGE"   => "",
    "VARIABLE"  => "",
    @_
  );
  
  ($self->{'ERROR'} = "No DIR specified" and return) unless ($in{'DIR'});
  ($self->{'ERROR'} = "No PACKAGE specified" and return) unless ($in{'PACKAGE'});
  ($self->{'ERROR'} = "No VARAIBLE specified" and return) unless ($in{'VARIABLE'} || $in{'TYPE'} eq "CLASS");
  
  my (@Records, %Index);
  
  opendir(DIR, $in{'DIR'}) || ($self->{'ERROR'} = "Error opening DIR. $!" and return);
  foreach my $file (grep(/\.pm\.pl$/, readdir(DIR))) {
    $file =~ m/^(.*)\.pm\.pl$/;
    
    require "$in{'DIR'}/$file" || ($self->{'ERROR'} = "Error requiring file. $!" and return);

    my $variable;
    if ($in{'TYPE'} eq "VAR") {
      eval("import $in{'PACKAGE'} qw(\$$in{'VARIABLE'});");
      $variable = eval("return \$$in{'VARIABLE'};");
    } else {
      $variable = eval("return ".$in{'PACKAGE'}."->new();");
    }

    my %RECORD;
    foreach my $key (keys %{ $variable }) {
      $RECORD{$key} = $variable->{$key};
    }
    
    push(@Records, \%RECORD);
    $Index{ $RECORD{'ID'} } = $#Records;
  }
  closedir(DIR);
  
  return (\@Records, \%Index);
}

1;